package com.zy.gmall0401.user.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.zy.gmall0401.bean.UserAddress;
import com.zy.gmall0401.bean.UserInfo;
import com.zy.gmall0401.user.mapper.UserAddressMapper;
import com.zy.gmall0401.user.mapper.UserMapper;
import com.zy.gmall0401.service.UserService;

import com.zy.gmall0401.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import redis.clients.jedis.Jedis;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author zy
 * @Date 2020/9/28 15:03
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserAddressMapper userAddressMapper;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public List<UserInfo> getUserInfoListAll() {
        List<UserInfo> userInfoList = userMapper.selectAll();
        return userInfoList;
    }

    public UserInfo getUserInfo(String id) {
        UserInfo userInfo = userMapper.selectByPrimaryKey(id);
        return userInfo;
    }

    @Override
    public void addUser(UserInfo userInfo) {
//        userMapper.insertSelective(userInfo);
        userMapper.insert(userInfo);

    }

    @Override
    public void updateUser(UserInfo userInfo) {
        userMapper.updateByPrimaryKeySelective(userInfo);
    }

    @Override
    public void updateUserByName(String name, UserInfo userInfo) {

        Example example = new Example(UserInfo.class);
        example.createCriteria().andEqualTo("name", name);
        userMapper.updateByExample(userInfo, example);

    }

    @Override
    public void delUser(UserInfo userInfo) {
        userMapper.deleteByPrimaryKey(userInfo);
    }

    @Override
    public void delUserid(String id) {
        userMapper.deleteByPrimaryKey(id);
    }


    //定义redis的key 值
    public String userKey_prefix = "user:";
    public String userinfoKey_suffix = ":info";
    public int userKey_timeOut = 60 * 60 * 24;

    @Override
    public UserInfo login(UserInfo userInfo) {
        //数据库保存的密码是MD5，需要先把前端给的密码转换成,md5后在去比较
        String passwd = userInfo.getPasswd();
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(passwd.getBytes());
        userInfo.setPasswd(md5DigestAsHex);
        UserInfo userInfo1 = userMapper.selectOne(userInfo);
        if (userInfo1 != null) {
            //查询数据库是有值的需要添加到缓存中去
            Jedis jedis = redisUtil.getJedis();
            //数据存入redis  需要做三件事   type  key  value
            jedis.setex(userKey_prefix + userInfo1.getId() + userinfoKey_suffix, userKey_timeOut,
                    JSON.toJSONString(userInfo1));

            jedis.close();
            return userInfo1;
        }
        return null;
    }


    //    验证redis的用户的信息
    @Override
    public Boolean verify(String userId) {
        Jedis jedis = redisUtil.getJedis();
        String key = userKey_prefix + userId + userinfoKey_suffix;
        Boolean exists = jedis.exists(key);
        if (exists) { //验证通过，延长登陆时间
            jedis.expire(userId, userKey_timeOut);
        }
        jedis.close();
        return exists;
    }

    public List<UserAddress> getUserAddressList(String userId) {

        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        List<UserAddress> userAddressList = userAddressMapper.select(userAddress);
        return userAddressList;
    }
}
